<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/column1';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();
	
	public static function levelUp($model, $upd_price)
	{

		$user = User::model()->findByPk(Yii::app()->user->getId());
		
		foreach($upd_price as $key => $val){
			$res = Resources::model()->find('id_settlement = '.$user->current_settlement.' AND id_resource = '.$key);
			if($res->count < $val*$model->lvl)
				return false;
		}
		
		foreach($upd_price as $key => $val)
		{
			$res = Resources::model()->find('id_settlement = '.$user->current_settlement.' AND id_resource = '.$key);
			$res->count -= $val*$model->lvl;
			$res->save();
		}
		
		$model->lvl++;
		$model->save();
		
		return true;
	}
	
	public static function getStrSelect()
	{
		if(Yii::app()->user->isGuest)
			return "";
			
		$cur_sett = User::model()->findByPk(Yii::app()->user->getId())->current_settlement;
		$strSelect = '<select class="select-query span2" onchange="changeSettlement(
																				this, 
																				\'' . Yii::app()->createUrl('profile/changeSettlement'). '\'
																					)">';
		
		if(!Yii::app()->user->isGuest)
		{
			$settl = Settlements::model()->findAll("id_user = " . Yii::app()->user->getId());
			foreach($settl as $s)
			{
				if($s->id == $cur_sett)
					$strSelect .= "<option value='" . $s->id . "' selected='selected'>" . $s->name . "</option>";	
				else
					$strSelect .= "<option value='" . $s->id . "'>" . $s->name . "</option>";
			}
		}
		
		$strSelect .= "</select>";
		

		return $strSelect;
	}
	
	public static function getArrayResources()
	{
		$res = array();
		
		if(Yii::app()->user->isGuest)
			return $res;
		
		$user = User::model()->findByPk(Yii::app()->user->getId());
		$settlement = $user->settlement;
		
		$settlement->maxResourceCount();
		$resources = Resources::model()->findAll("id_settlement = " . $settlement->id);
		
		foreach($resources as $resource)
		{
			$res[] = array('label' => $settlement->maxResources[$resource->id_resource][1] . ": " . 
									  $resource->count . "/" . 
									  $settlement->maxResources[$resource->id_resource][0]
						  );
			$res[] = '---';
		}
		
		return $res;
	}
	
	protected function beforeAction($action)
	{
		$this->updateSettlement();
		return true;
	}
	
	public function updateSettlement()
	{
	
		if(Yii::app()->user->isGuest)
			return;
	
		$user = User::model()->findByPk(Yii::app()->user->getId());
		$res = Resources::model()->findAll("id_settlement = " . $user->current_settlement);
		$fields = $user->settlement->fields;
		$buildings = $user->settlement->buildings;
		
		$settlement = $user->settlement;
		$settlement->maxResourceCount();
		
		foreach($res as  $r)
		{
			$hours = (int)((time() - $r->last_update)/60/60);
			if($hours == 0)
				continue;
			
			foreach($fields as $field)
			{
				if($field->type->id_resource == $r->id_resource)
				{
					$r->count += $field->type->count * $field->lvl * $hours;
					
					if($r->count > $settlement->maxResources[$r->id_resource][0])
					{
						$r->count = $settlement->maxResources[$r->id_resource][0];
					}	
				}
			}
			
			foreach($buildings as $build)
			{
				if($build->type->id == 3 && $r->id_resource == 4)
				{
					$r->count += 20 * $build->lvl * $hours;
					
					if($r->count > $settlement->maxResources[$r->id_resource][0])
					{
						$r->count = $settlement->maxResources[$r->id_resource][0];
					}	
				}
			}
			
			
			$r->last_update = $r->last_update + ($hours * 3600);
			$r->save();
		}
		
		
		
	}
}